using FireStarterModeller.BO;
using Habanero.BO;
using NUnit.Framework;

namespace FireStarterModeller.Test.BO
{
    [TestFixture]
    public class TestUIColumnLayoutInfo : TestBase
    {
        [SetUp]
        public override void SetupTest()
        {
            //Runs every time that any testmethod is executed
            base.SetupTest();
        }

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            //Code that is executed before any test is run in this class. If multiple tests
            // are executed then it will still only be called once.
        }

        [TearDown]
        public override void TearDownTest()
        {
            //runs every time any testmethod is complete
            base.TearDownTest();
        }

        [Test]
        public void Test_InsertNewUIColumnLayoutInfo()
        {
            //---------------Set up test pack-------------------
            UIColumnLayout uiColumnLayout = TestUtils.CreateUnsavedValidUIColumnLayoutInfo();

            //---------------Assert Precondition----------------
            Assert.IsTrue(uiColumnLayout.Status.IsNew);

            //---------------Execute Test ----------------------
            uiColumnLayout.Save();

            //---------------Test Result -----------------------
            Assert.IsFalse(uiColumnLayout.Status.IsNew);
        }

        [Test]
        public void Test_DeleteUIColumnLayoutInfo()
        {
            //---------------Set up test pack-------------------
            UIColumnLayout uiColumnLayout = TestUtils.CreateSavedUIColumnLayoutInfo();

            //---------------Assert Precondition----------------
            Assert.IsFalse(uiColumnLayout.Status.IsNew);
            string message;
            Assert.IsTrue(uiColumnLayout.IsDeletable(out message));

            //---------------Execute Test ----------------------
            uiColumnLayout.MarkForDelete();
            uiColumnLayout.Save();

            //---------------Test Result -----------------------  
            Assert.IsTrue(uiColumnLayout.Status.IsNew);
            Assert.IsTrue(uiColumnLayout.Status.IsDeleted);
        }

        [Test]
        public void Test_CreateField()
        {
            //---------------Set up test pack-------------------
            BusinessObjectManager.Instance.ClearLoadedObjects();
            UIColumnLayout uiColumnLayout = TestUtils.CreateSavedUIColumnLayoutInfo();
            UIField uiField = uiColumnLayout.UIFields.CreateBusinessObject();
            TestUtils.UpdateUIFieldInfoWithValidData(uiField);
            
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, uiColumnLayout.UIFields.CreatedBusinessObjects.Count);
            Assert.AreEqual(1, uiColumnLayout.UIFields.Count);

            //Assert.AreEqual(1, businessObjectLoader.GetMemoryDatabase().Count);
            //---------------Execute Test ----------------------
            uiField.Save();

            //---------------Test Result -----------------------
            //Assert.AreEqual(2, businessObjectLoader.GetMemoryDatabase().Count);

            Assert.AreEqual(0, uiColumnLayout.UIFields.CreatedBusinessObjects.Count);
            Assert.AreEqual(1, uiColumnLayout.UIFields.Count);
        }

        [Test]
        public void Test_DeletesFields()
        {
            //---------------Set up test pack-------------------
            UIColumnLayout uiColumnLayout = TestUtils.CreateSavedUIColumnLayoutInfo();
            BusinessObjectManager.Instance.ClearLoadedObjects();
            UIField uiField = uiColumnLayout.UIFields.CreateBusinessObject();
            TestUtils.UpdateUIFieldInfoWithValidData(uiField);
            uiField.UIColumnLayout = uiColumnLayout;
            UIField uiFieldInfo2 = uiColumnLayout.UIFields.CreateBusinessObject();
            TestUtils.UpdateUIFieldInfoWithValidData(uiFieldInfo2);
            uiFieldInfo2.UIColumnLayout = uiColumnLayout;
            uiField.Save(); 
            uiFieldInfo2.Save();
            //---------------Execute Test ----------------------
            uiColumnLayout.MarkForDelete();
            uiColumnLayout.Save();
            //---------------Test Result -----------------------
            Assert.IsTrue(uiColumnLayout.Status.IsDeleted);
            //Assert.IsTrue(uiFieldInfo2.Status.IsDeleted);
            //Assert.IsTrue(uiField.Status.IsDeleted);
            //---------------Tear Down -------------------------

        }

        [Test]
        public void Test_UpdateUIColumnLayoutInfo()
        {
            //---------------Set up test pack-------------------
            UIColumnLayout uiColumnLayout = TestUtils.CreateSavedUIColumnLayoutInfo();

            //---------------Assert Precondition----------------
            Assert.IsFalse(uiColumnLayout.Status.IsNew);
            string message;
            Assert.IsTrue(uiColumnLayout.IsEditable(out message));

            //---------------Execute Test ----------------------
            uiColumnLayout.Width = 100;
            uiColumnLayout.Save();
            //---------------Test Result -----------------------
            Assert.IsFalse(uiColumnLayout.Status.IsNew);
            Assert.AreEqual(100, uiColumnLayout.Width);
        }
    }
}